// pages/category/category.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    categoryList: [],
    showContentList: [],
    sumContentList: [],
    curindex: 0,
    network: getApp().globalData["network"]
    
  },
  queryContent: function () {
    var that = this;
    // 显示内容
    wx.request({
      url: "http://" + that.data.network +"/front/catShowContent.php",
      method: "post",
      datatype: "json",
      success: function (data) {
        data = data['data'];
        var index = that.data.curindex;
        var curdata = data[index][index];
        that.setData({
          showContentList: curdata,
          sumContentList: data
        });
        wx.setStorageSync("sumContentList", { time: Date.now(), data: that.data.sumContentList });
      }
    });
  },
  intocart:function(e){
    var that = this;
    var id= e.currentTarget.dataset.index;
    var user_id = wx.getStorageSync("id");
    wx.request({
      url:"http://" + that.data.network +"/front/intocart.php",
      method: "post",
      datatype: "json",
      data:{
        user_id:user_id,
        goods_id:id
      },
      header:{
        "Content-Type":"application/x-www-form-urlencoded"
      },
      success: function (data) {
        // that.setData({
        //   categoryList: data.data
        // });
        console.log(data);
        wx.showToast({
          title:"加入购物车",
          icon:"success",
          duration:1000
        });
      }
    });

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log("onload");
    // 广告
    wx.request({
      url: "http://" + that.data.network +"/front/categoryQueryAll.php",
      method: "post",
      datatype: "json",
      success: function (data) {
        that.setData({
          categoryList: data.data
        });
        console.log(that.data.categoryList);
      }
    });
    that.queryContent();

  },
  handleTab: function (event) {
    var { index } = event.currentTarget.dataset;
    var that = this;
    const datas = wx.getStorageSync("sumContentList");
    this.setData({
      curindex: index
    });
    if (datas) {
      if (Date.now() - datas['data']['time'] > 1000 * 60) {
        that.queryContent();
      } else {
        that.setData({
          sumContentList: datas['data'],
          showContentList: datas['data'][index][index]
        });
      }
    } else {
      that.queryContent();
    }

  }
  ,todtl:function(event){
    var index =event.currentTarget.dataset.index;
    console.log(index);
    wx.navigateTo({
      url:"/pages/detail/detail",
      events:{
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        acceptDataFromOpenedPage: function(data) {
         // console.log(data)
        },
      },
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage',index)
      }
    });
  },
  

})